<template>
    <div style="text-align: left">
        <el-button type="primary" :icon="ArrowLeft" @click="back()">返回搜索页面</el-button>
    </div>
    <img src=".././assets/logo2.png" style="width: 35%;margin-top: -3%">
    <div class="container">
        <div class="form-container sign-in-container">
            <el-form class="form" :model="form" label-width="20%" status-icon>
                <h1>登 录</h1>
                <div class="social-container">
                    <a class="social"><el-icon><ChatDotRound /></el-icon></a>
                    <a class="social"><el-icon><Aim /></el-icon></a>
                    <a class="social"><el-icon><Reading /></el-icon></a>
                </div>
                <input v-model="form.username" placeholder="请输入用户名/邮箱" clearable maxlength="30">
                <input v-model="form.password" placeholder="请输入密码" maxlength="20" type="password">
                <a>忘记密码?</a>
                <el-button @click="login">登录</el-button>
            </el-form>
        </div>
        <div class="overlay-container">
            <div class="overlay">
                <div class="overlay-panel overlay-right">
                    <h1>Hello, Friend!</h1>
                    <p>欢迎加入我们！请输入你的信息</p>
                    <button class="ghost" id="signUp" @click="toRegister()">注 册</button>
                </div>
            </div>
        </div>
    </div>
    <footer>
        <p>
            网站创建者<i class="fa fa-heart"></i>为
            <a target="_blank" href="https://florin-pop.com">软件工程管理 G07</a>
            如果有任何意见或建议请联系我们
            <a target="_blank" href="https://www.florin-pop.com/blog/2019/03/double-slider-sign-in-up-form/">3210104842@zju.edu.cn</a>
        </p>
    </footer>
</template>

<style scoped>

@import url('https://fonts.googleapis.com/css?family=Montserrat:400,800');

* {
    box-sizing: border-box;
}

body {
    background: #f6f5f7;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    font-family: 'Montserrat', sans-serif;
    height: 100vh;
    margin: -20px 0 50px;
}

h1 {
    font-weight: bold;
    margin: 0;
}

h2 {
    text-align: center;
}

p {
    font-size: 14px;
    font-weight: 100;
    line-height: 20px;
    letter-spacing: 0.5px;
    margin: 20px 0 30px;
}

span {
    font-size: 12px;
}

a {
    color: #333;
    font-size: 14px;
    text-decoration: none;
    margin: 15px 0;
}

button {
    border-radius: 20px;
    border: 1px solid #c74601;
    background-color: #ff7c2b;
    color: #FFFFFF;
    font-size: 12px;
    font-weight: bold;
    padding: 12px 45px;
    letter-spacing: 1px;
    text-transform: uppercase;
    transition: transform 80ms ease-in;
}

button:active {
    transform: scale(0.95);
}

button:focus {
    outline: none;
}

button.ghost {
    background-color: transparent;
    border-color: #FFFFFF;
}

.form{
    background-color: #FFFFFF;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    padding: 0 50px;
    height: 100%;
    text-align: center;
}

input {
    background-color: #eee;
    border: none;
    padding: 12px 15px;
    margin: 8px 0;
    width: 100%;
}

.container {
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 14px 28px rgba(0,0,0,0.25),
    0 10px 10px rgba(0,0,0,0.22);
    position: relative;
    overflow: hidden;
    width: 768px;
    max-width: 100%;
    min-height: 480px;
    margin-left: 17%;
}

.form-container {
    position: absolute;
    top: 0;
    height: 100%;
    transition: all 0.6s ease-in-out;
}

.sign-in-container {
    left: 0;
    width: 50%;
    z-index: 2;
}

.container.right-panel-active .sign-in-container {
    transform: translateX(100%);
}

.container.right-panel-active .sign-up-container {
    transform: translateX(100%);
    opacity: 1;
    z-index: 5;
    animation: show 0.6s;
}

@keyframes show {
    0%, 49.99% {
        opacity: 0;
        z-index: 1;
    }

    50%, 100% {
        opacity: 1;
        z-index: 5;
    }
}

.overlay-container {
    position: absolute;
    top: 0;
    left: 50%;
    width: 50%;
    height: 100%;
    overflow: hidden;
    transition: transform 0.6s ease-in-out;
    z-index: 100;
}

.container.right-panel-active .overlay-container{
    transform: translateX(-100%);
}

.overlay {
    background: #ffa641;
    background: -webkit-linear-gradient(to right, #af5716, #d58b38);
    background: linear-gradient(to right, #985202, #ff7441);
    background-repeat: no-repeat;
    background-size: cover;
    background-position: 0 0;
    color: #FFFFFF;
    position: relative;
    left: -50%;
    height: 100%;
    width: 200%;
    transform: translateX(0);
    transition: transform 0.6s ease-in-out;
}

.container.right-panel-active .overlay {
    transform: translateX(50%);
}

.overlay-panel {
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    padding: 0 40px;
    text-align: center;
    top: 0;
    height: 100%;
    width: 50%;
    transform: translateX(50%);
    transition: transform 0.6s ease-in-out;
}

.overlay-left {
    transform: translateX(-20%);
}

.container.right-panel-active .overlay-left {
    transform: translateX(0);
}

.container.right-panel-active .overlay-right {
    transform: translateX(20%);
}

.social-container {
    margin: 20px 0;
}

.social-container a {
    border: 1px solid #DDDDDD;
    border-radius: 50%;
    display: inline-flex;
    justify-content: center;
    align-items: center;
    margin: 0 5px;
    height: 40px;
    width: 40px;
}

footer {
    background-color: #222;
    color: #fff;
    font-size: 14px;
    bottom: 0;
    position: fixed;
    left: 0;
    right: 0;
    text-align: center;
    z-index: 999;
}

footer p {
    margin: 10px 0;
}

footer i {
    color: #862400;
}

footer a {
    color: #54bf3c;
    text-decoration: none;
}
</style>

<script setup>
import {post} from "@/net";
import {reactive, ref} from "vue";
import {Aim, ArrowLeft, ChatDotRound, Reading} from "@element-plus/icons-vue";
import router, {previousPageUrl} from "@/router";
import {ElMessage} from "element-plus";
import {AES_Encrypt} from "@/encoder";
import axios from "axios";
const userId = ref('-1');
//from表单校验
const form = reactive({
    username:'',
    password:''
})
const toRegister = () => {
    router.replace('/register');
}
const login = ()=> {
    if (!form.username) {
        ElMessage.warning('请填写邮箱或者用户名')
    } else if (!form.password) {
        ElMessage.warning('请填写密码')
    } else if (form.username.length < 5 || form.password.length < 5) {
        ElMessage.warning('用户名或密码一定要在5位以上')
    } else if (form.username.length > 30 || form.password.length > 30) {
        ElMessage.warning('用户名或密码过长，不宜超过30个字符')
    } else {
        form.password = AES_Encrypt(form.password)
        axios.post('http://localhost:8081/user/login', {
            name: form.username,
            passWord: form.password,
        }).then((data) => {
            if(data.data.code === 1) {
                console.log(data.data.data)
                ElMessage.success("登录成功")
                userId.value = data.data.data;
                back()
            }else{
                form.password = ''
                ElMessage.success("请检查用户名和邮箱")
            }
            }).catch(
            (message)=>{
                console.log(form.username)
                return ElMessage.error(message);
            });
    }
}
function modifyUrl(url) {
    // 查找是否存在 /log/-1/
    const indexOfLog = url.indexOf('/log/-1/');
    if (indexOfLog !== -1) {
        // 替换 /log/-1/ 为 /log/userId
        return url.substring(0, indexOfLog) + `/log/${userId.value}` + url.substring(indexOfLog + 7);
    } else {
        // 如果不存在 log/-1/，在 URL 后面加上 log/userId
        return `/log/${userId.value}`;
    }
}

const back = () => {
    // 获取前一个页面的 url
    let url = modifyUrl(previousPageUrl)
    console.log(url)
    router.push(url)
}
</script>
